package com.zenchn.controller;

import com.zenchn.annotation.OperLog;
import com.zenchn.entity.HttpDataResult;
import com.zenchn.entity.JsonResult;
import com.zenchn.entity.ReportStatisticsVO;
import com.zenchn.enums.CustomerLevelTypeEnum;
import com.zenchn.enums.CustomerTypeEnum;
import com.zenchn.enums.OperLogActionEnum;
import com.zenchn.exception.CustomException;
import com.zenchn.external.service.FileGenericStrategy;
import com.zenchn.model.TMiRegionDetail;
import com.zenchn.model.TMiRegionDetailExample;
import com.zenchn.model.TSysAccount;
import com.zenchn.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 报表统计控制器
 *
 * @author panbingqi
 * @date 2021-06-04 17:34
 */
@RestController
@RequestMapping(value = "reportStatisticsWeb")
@Api(value = "报表统计控制器", tags = "信息管理-报表统计")
@RequiresPermissions("report:statistics:data")
public class ReportStatisticsWebController {
    private final String MODULE = "信息管理>报表统计";

    @Resource
    private RegionDetailService regionDetailService;

    @Resource
    private CustomerService customerService;

    @Resource
    private FileGenericStrategy fileGenericStrategy;

    @Resource
    private AccountService accountService;

    /**
     * 获取客户报表统计信息
     *
     * @param regionId
     * @param areasCode
     * @param accountId
     * @param customerType
     * @param startTime
     * @param endTime
     * @param page
     * @param pageSize
     * @param request
     * @param response
     * @return
     */
    @OperLog(module = MODULE, action = OperLogActionEnum.QUERY, detail = "获取客户报表统计信息")
    @ApiOperation(value = "获取客户报表统计信息", notes = "获取客户报表统计信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "regionId", value = "所属大区"),
            @ApiImplicitParam(dataType = "String", name = "areasCode", value = "所属省份"),
            @ApiImplicitParam(dataType = "String", name = "accountId", value = "所属用户"),
            @ApiImplicitParam(dataType = "String", name = "customerType", value = "类型"),
            @ApiImplicitParam(dataType = "Date", name = "startTime", value = "参数名称，开始时间"),
            @ApiImplicitParam(dataType = "Date", name = "endTime", value = "参数名称，结束时间"),
            @ApiImplicitParam(dataType = "Integer", name = "page", value = "当前第page页"),
            @ApiImplicitParam(dataType = "Integer", name = "pageSize", value = "每页pageSize数量")
    })
    @GetMapping(value = "/customerStatisticsList")
    @RequiresPermissions("report:statistics:data")
    public ResponseEntity<HttpDataResult<List<ReportStatisticsVO>>> customerStatisticsList(
            @RequestParam(required = false, value = "regionId") String regionId,
            @RequestParam(required = false, value = "areasCode") String areasCode,
            @RequestParam(required = false, value = "accountId") String accountId,
            @RequestParam(required = false, value = "customerType") String customerType,
            @RequestParam(value = "startTime") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
            @RequestParam(value = "endTime") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
            @RequestParam(required = false, value = "page", defaultValue = "1") Integer page,
            @RequestParam(required = false, value = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpDataResult<List<ReportStatisticsVO>> result = new HttpDataResult<>(request.getRequestURI());

        List<String> areasCodeList = null;
        if (regionId != null && !"".equals(regionId.trim())) {
            areasCodeList = getAreasCodeList(regionId);
        }

        LocalDateTime startDate = null;
        if (startTime != null) {
            startDate = LocalDateTime.of(startTime, LocalTime.MIN);
        }
        LocalDateTime endDate = null;
        if (endTime != null) {
            endDate = LocalDateTime.of(endTime, LocalTime.MAX);
        }

        List<ReportStatisticsVO> resultList = new ArrayList<>();
        Map<String, ReportStatisticsVO> resultMap = new HashMap<>();

        //获取本期新增客户数量
        List<ReportStatisticsVO> curCustomerNumList = customerService.selectCurCustomerNum(areasCodeList, areasCode, accountId, customerType, startDate, endDate);
        if (curCustomerNumList != null && !curCustomerNumList.isEmpty()) {
            for (ReportStatisticsVO reportStatisticsVO : curCustomerNumList) {
                String key = reportStatisticsVO.getAccountId() + ":" + reportStatisticsVO.getCustomerType() + ":" + "1";
                reportStatisticsVO.setLevel(CustomerLevelTypeEnum.ONE.getType());
                if (!resultMap.containsKey(key)) {
                    resultMap.put(key, reportStatisticsVO);
                }
            }
        }
        //获取期末总客户
        List<ReportStatisticsVO> finalCustomerAllNumList = customerService.selectFinalCustomerAllNum(areasCodeList, areasCode, accountId, customerType, endDate);
        //获取期末客户等级分布
        List<ReportStatisticsVO> finalCustomerLevelNumList = customerService.selectFinalCustomerLevelNum(areasCodeList, areasCode, accountId, customerType, endDate);
        //设置期末客户等级为1级数量
        Map<String, Integer> map = new HashMap<>();
        if (finalCustomerLevelNumList != null && !finalCustomerLevelNumList.isEmpty()) {
            for (ReportStatisticsVO reportStatisticsVO : finalCustomerLevelNumList) {
                String key = reportStatisticsVO.getAccountId() + ":" + reportStatisticsVO.getCustomerType();
                if (map.containsKey(key)) {
                    Integer integer = map.get(key);
                    integer += reportStatisticsVO.getFinalCustomerLevelNum();
                    map.put(key, integer);
                } else {
                    map.put(key, reportStatisticsVO.getFinalCustomerLevelNum());
                }
            }
        }
        if (finalCustomerAllNumList != null && !finalCustomerAllNumList.isEmpty()) {
            for (ReportStatisticsVO reportStatisticsVO : finalCustomerAllNumList) {
                String key = reportStatisticsVO.getAccountId() + ":" + reportStatisticsVO.getCustomerType();
                reportStatisticsVO.setLevel(CustomerLevelTypeEnum.ONE.getType());
                if (map.containsKey(key)) {
                    reportStatisticsVO.setFinalCustomerLevelNum(reportStatisticsVO.getFinalCustomerLevelNum() - map.get(key));
                }
                if (resultMap.containsKey(key + ":" + "1")) {
                    ReportStatisticsVO statisticsVO = resultMap.get(key + ":" + "1");
                    statisticsVO.setFinalCustomerLevelNum(reportStatisticsVO.getFinalCustomerLevelNum());
                    resultMap.put(key + ":" + "1", statisticsVO);
                } else {
                    resultMap.put(key + ":" + "1", reportStatisticsVO);
                }
            }
        }
        //设置期末客户等级为2-5级数量
        if (finalCustomerLevelNumList != null && !finalCustomerLevelNumList.isEmpty()) {
            for (ReportStatisticsVO reportStatisticsVO : finalCustomerLevelNumList) {
                String key = reportStatisticsVO.getAccountId() + ":" + reportStatisticsVO.getCustomerType() + ":" + reportStatisticsVO.getLevel();
                if (resultMap.containsKey(key)) {
                    ReportStatisticsVO reportStatistics = resultMap.get(key);
                    reportStatistics.setFinalCustomerLevelNum(reportStatisticsVO.getFinalCustomerLevelNum());
                    resultMap.put(key, reportStatistics);
                } else {
                    resultMap.put(key, reportStatisticsVO);
                }
            }
        }
        //最新客户等级分布
        List<ReportStatisticsVO> newCustomerLevelNumList = customerService.selectNewCustomerLevelNum(areasCodeList, areasCode, accountId, customerType);
        if (newCustomerLevelNumList != null && !newCustomerLevelNumList.isEmpty()) {
            for (ReportStatisticsVO reportStatisticsVO : newCustomerLevelNumList) {
                String key = reportStatisticsVO.getAccountId() + ":" + reportStatisticsVO.getCustomerType() + ":" + reportStatisticsVO.getLevel();
                if (resultMap.containsKey(key)) {
                    ReportStatisticsVO reportStatistics = resultMap.get(key);
                    reportStatistics.setNewCustomerLevelNum(reportStatisticsVO.getNewCustomerLevelNum());
                    resultMap.put(key, reportStatistics);
                } else {
                    resultMap.put(key, reportStatisticsVO);
                }
            }
        }
        //本期晋级数量
        List<ReportStatisticsVO> CurCustomerUpgradeNumList = customerService.selectCurCustomerUpgradeNum(areasCodeList, areasCode, accountId, customerType, startDate, endDate);
        if (CurCustomerUpgradeNumList != null && !CurCustomerUpgradeNumList.isEmpty()) {
            for (ReportStatisticsVO reportStatisticsVO : CurCustomerUpgradeNumList) {
                String key = reportStatisticsVO.getAccountId() + ":" + reportStatisticsVO.getCustomerType() + ":" + reportStatisticsVO.getLevel();
                if (resultMap.containsKey(key)) {
                    ReportStatisticsVO reportStatistics = resultMap.get(key);
                    reportStatistics.setCurCustomerUpgradeNum(reportStatisticsVO.getCurCustomerUpgradeNum());
                    resultMap.put(key, reportStatistics);
                } else {
                    resultMap.put(key, reportStatisticsVO);
                }
            }
        }
        //本期业绩完成情况
        List<ReportStatisticsVO> curCustomerFinishNumList = customerService.selectCurCustomerFinishNum(areasCodeList, areasCode, accountId, customerType, startDate, endDate);
        if (curCustomerFinishNumList != null && !curCustomerFinishNumList.isEmpty()) {
            for (ReportStatisticsVO reportStatisticsVO : curCustomerFinishNumList) {
                String key = reportStatisticsVO.getAccountId() + ":" + reportStatisticsVO.getCustomerType() + ":" + reportStatisticsVO.getLevel();
                if (resultMap.containsKey(key)) {
                    ReportStatisticsVO reportStatistics = resultMap.get(key);
                    reportStatistics.setCurCustomerFinishNum(reportStatisticsVO.getCurCustomerFinishNum());
                    resultMap.put(key, reportStatistics);
                } else {
                    resultMap.put(key, reportStatisticsVO);
                }
            }
        }

        //结果封装
        resultMap.forEach((key, value) -> {
            //设置账户名称
            TSysAccount account = accountService.selectByPrimaryKey(value.getAccountId());
            if (account != null) {
                value.setAccountName(account.getRealName());
            }
            //设置类型名称
            value.setCustomerTypeName(CustomerTypeEnum.getAppDesc(value.getCustomerType()));
            //设置等级名称
            if (CustomerTypeEnum.HOSPITAL.getType().equals(value.getCustomerType())) {
                value.setLevelName(CustomerLevelTypeEnum.getHospitalDesc(value.getLevel()));
            }
            if (CustomerTypeEnum.AGENT.getType().equals(value.getCustomerType())) {
                value.setLevelName(value.getLevel() + "");
            }

            resultList.add(value);
        });
        //排序
        List<ReportStatisticsVO> finalResultList = resultList.stream().sorted(Comparator.comparing(ReportStatisticsVO::getAccountId).thenComparing(ReportStatisticsVO::getCustomerType).thenComparing(ReportStatisticsVO::getLevel)).collect(Collectors.toList());

        result.setData(finalResultList);

        result.setMessage("获取客户报表统计信息成功.");
        result.setStatus(HttpStatus.OK.value());
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 获取省份信息
     *
     * @param regionId
     * @return
     */
    private List<String> getAreasCodeList(String regionId) {
        List<String> areasCodeList = new ArrayList<>();
        TMiRegionDetailExample regionDetailExample = new TMiRegionDetailExample();
        regionDetailExample.createCriteria().andRegionIdEqualTo(regionId);

        List<TMiRegionDetail> regionDetailList = regionDetailService.selectByExample(regionDetailExample);
        if (regionDetailList != null && !regionDetailList.isEmpty()) {
            areasCodeList.addAll(regionDetailList.stream().map(TMiRegionDetail::getAreasCode).collect(Collectors.toList()));
        }
        return areasCodeList;
    }


    /**
     * 导出客户报表统计信息列表
     *
     * @param regionId
     * @param areasCode
     * @param accountId
     * @param customerType
     * @param startTime
     * @param endTime
     * @param request
     * @param response
     * @return
     */
    @OperLog(module = MODULE, action = OperLogActionEnum.QUERY, detail = "导出客户报表统计信息列表")
    @ApiOperation(value = "导出客户报表统计信息列表", notes = "导出客户报表统计信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "regionId", value = "所属大区"),
            @ApiImplicitParam(dataType = "String", name = "areasCode", value = "所属省份"),
            @ApiImplicitParam(dataType = "String", name = "accountId", value = "所属用户"),
            @ApiImplicitParam(dataType = "String", name = "customerType", value = "类型"),
            @ApiImplicitParam(dataType = "Date", name = "startTime", value = "参数名称，开始时间", required = true),
            @ApiImplicitParam(dataType = "Date", name = "endTime", value = "参数名称，结束时间", required = true),
    })
    @GetMapping(value = "/export")
    @RequiresPermissions("report:statistics:data:export")
    public ResponseEntity<HttpDataResult<String>> export(
            @RequestParam(required = false, value = "regionId") String regionId,
            @RequestParam(required = false, value = "areasCode") String areasCode,
            @RequestParam(required = false, value = "accountId") String accountId,
            @RequestParam(required = false, value = "customerType") String customerType,
            @RequestParam(value = "startTime") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
            @RequestParam(value = "endTime") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
            HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpDataResult<String> result = new HttpDataResult<>(request.getRequestURI());

        List<String> areasCodeList = null;
        if (regionId != null) {
            areasCodeList = getAreasCodeList(regionId);
        }

        LocalDateTime startDate = null;
        if (startTime != null) {
            startDate = LocalDateTime.of(startTime, LocalTime.MIN);
        }
        LocalDateTime endDate = null;
        if (endTime != null) {
            endDate = LocalDateTime.of(endTime, LocalTime.MAX);
        }

        List<ReportStatisticsVO> resultList = new ArrayList<>();
        Map<String, ReportStatisticsVO> resultMap = new HashMap<>();

        //获取本期新增客户数量
        List<ReportStatisticsVO> curCustomerNumList = customerService.selectCurCustomerNum(areasCodeList, areasCode, accountId, customerType, startDate, endDate);
        if (curCustomerNumList != null && !curCustomerNumList.isEmpty()) {
            for (ReportStatisticsVO reportStatisticsVO : curCustomerNumList) {
                String key = reportStatisticsVO.getAccountId() + ":" + reportStatisticsVO.getCustomerType() + ":" + "1";
                reportStatisticsVO.setLevel(CustomerLevelTypeEnum.ONE.getType());
                if (!resultMap.containsKey(key)) {
                    resultMap.put(key, reportStatisticsVO);
                }
            }
        }
        //获取期末客户等级分布
        List<ReportStatisticsVO> finalCustomerLevelNumList = customerService.selectFinalCustomerLevelNum(areasCodeList, areasCode, accountId, customerType, endDate);
        if (finalCustomerLevelNumList != null && !finalCustomerLevelNumList.isEmpty()) {
            for (ReportStatisticsVO reportStatisticsVO : finalCustomerLevelNumList) {
                String key = reportStatisticsVO.getAccountId() + ":" + reportStatisticsVO.getCustomerType() + ":" + reportStatisticsVO.getLevel();
                if (resultMap.containsKey(key)) {
                    ReportStatisticsVO reportStatistics = resultMap.get(key);
                    reportStatistics.setFinalCustomerLevelNum(reportStatisticsVO.getFinalCustomerLevelNum());
                    resultMap.put(key, reportStatistics);
                } else {
                    resultMap.put(key, reportStatisticsVO);
                }
            }
        }
        //最新客户等级分布
        List<ReportStatisticsVO> newCustomerLevelNumList = customerService.selectNewCustomerLevelNum(areasCodeList, areasCode, accountId, customerType);
        if (newCustomerLevelNumList != null && !newCustomerLevelNumList.isEmpty()) {
            for (ReportStatisticsVO reportStatisticsVO : newCustomerLevelNumList) {
                String key = reportStatisticsVO.getAccountId() + ":" + reportStatisticsVO.getCustomerType() + ":" + reportStatisticsVO.getLevel();
                if (resultMap.containsKey(key)) {
                    ReportStatisticsVO reportStatistics = resultMap.get(key);
                    reportStatistics.setNewCustomerLevelNum(reportStatisticsVO.getNewCustomerLevelNum());
                    resultMap.put(key, reportStatistics);
                } else {
                    resultMap.put(key, reportStatisticsVO);
                }
            }
        }
        //本期晋级数量
        List<ReportStatisticsVO> CurCustomerUpgradeNumList = customerService.selectCurCustomerUpgradeNum(areasCodeList, areasCode, accountId, customerType, startDate, endDate);
        if (CurCustomerUpgradeNumList != null && !CurCustomerUpgradeNumList.isEmpty()) {
            for (ReportStatisticsVO reportStatisticsVO : CurCustomerUpgradeNumList) {
                String key = reportStatisticsVO.getAccountId() + ":" + reportStatisticsVO.getCustomerType() + ":" + reportStatisticsVO.getLevel();
                if (resultMap.containsKey(key)) {
                    ReportStatisticsVO reportStatistics = resultMap.get(key);
                    reportStatistics.setCurCustomerUpgradeNum(reportStatisticsVO.getCurCustomerUpgradeNum());
                    resultMap.put(key, reportStatistics);
                } else {
                    resultMap.put(key, reportStatisticsVO);
                }
            }
        }
        //本期业绩完成情况
        List<ReportStatisticsVO> curCustomerFinishNumList = customerService.selectCurCustomerFinishNum(areasCodeList, areasCode, accountId, customerType, startDate, endDate);
        if (curCustomerFinishNumList != null && !curCustomerFinishNumList.isEmpty()) {
            for (ReportStatisticsVO reportStatisticsVO : curCustomerFinishNumList) {
                String key = reportStatisticsVO.getAccountId() + ":" + reportStatisticsVO.getCustomerType() + ":" + reportStatisticsVO.getLevel();
                if (resultMap.containsKey(key)) {
                    ReportStatisticsVO reportStatistics = resultMap.get(key);
                    reportStatistics.setCurCustomerFinishNum(reportStatisticsVO.getCurCustomerFinishNum());
                    resultMap.put(key, reportStatistics);
                } else {
                    resultMap.put(key, reportStatisticsVO);
                }
            }
        }

        //结果封装
        resultMap.forEach((key, value) -> {
            //设置账户名称
            TSysAccount account = accountService.selectByPrimaryKey(value.getAccountId());
            if (account != null) {
                value.setAccountName(account.getRealName());
            }
            //设置类型名称
            value.setCustomerTypeName(CustomerTypeEnum.getAppDesc(value.getCustomerType()));
            //设置等级名称
            if (CustomerTypeEnum.HOSPITAL.getType().equals(value.getCustomerType())) {
                value.setLevelName(CustomerLevelTypeEnum.getHospitalDesc(value.getLevel()));
            }
            if (CustomerTypeEnum.AGENT.getType().equals(value.getCustomerType())) {
                value.setLevelName(value.getLevel() + "");
            }

            resultList.add(value);
        });
        //排序
        List<ReportStatisticsVO> finalResultList = resultList.stream().sorted(Comparator.comparing(ReportStatisticsVO::getAccountId).thenComparing(ReportStatisticsVO::getCustomerType).thenComparing(ReportStatisticsVO::getLevel)).collect(Collectors.toList());
        if (null == finalResultList || finalResultList.isEmpty()) {
            throw new CustomException("未查询到相关客户信息，无法导出！");
        }

        //设置导出相关信息
        XSSFWorkbook wb = new XSSFWorkbook();

        XSSFSheet sheet = wb.createSheet("客户信息统计");

        sheet.setColumnWidth(0, (25 * 256));
        sheet.setColumnWidth(1, (25 * 256));
        sheet.setColumnWidth(2, (15 * 256));
        sheet.setColumnWidth(3, (35 * 256));
        sheet.setColumnWidth(4, (35 * 256));
        sheet.setColumnWidth(5, (35 * 256));
        sheet.setColumnWidth(6, (35 * 256));
        sheet.setColumnWidth(7, (35 * 256));


        XSSFRow row0 = sheet.createRow(0);
        row0.setHeightInPoints(28);

        XSSFFont tableHeaderFont = wb.createFont();
        tableHeaderFont.setFontHeightInPoints((short) 12);
        tableHeaderFont.setBold(true);
        XSSFCellStyle tableHeaderStyle = wb.createCellStyle();
        tableHeaderStyle.setFont(tableHeaderFont);
        // 下边框
        tableHeaderStyle.setBorderBottom(BorderStyle.THIN);
        // 左边框
        tableHeaderStyle.setBorderLeft(BorderStyle.THIN);
        // 右边框
        tableHeaderStyle.setBorderRight(BorderStyle.THIN);
        // 上边框
        tableHeaderStyle.setBorderTop(BorderStyle.THIN);
        tableHeaderStyle.setAlignment(HorizontalAlignment.CENTER);
        tableHeaderStyle.setVerticalAlignment(VerticalAlignment.CENTER);


        createCell(row0, 0, tableHeaderStyle, "业务员");
        createCell(row0, 1, tableHeaderStyle, "客户类型");
        createCell(row0, 2, tableHeaderStyle, "等级");
        createCell(row0, 3, tableHeaderStyle, "本期新增客户数量");
        createCell(row0, 4, tableHeaderStyle, "期末客户等级分布");
        createCell(row0, 5, tableHeaderStyle, "最新客户等级分布");
        createCell(row0, 6, tableHeaderStyle, "本期晋级数量");
        createCell(row0, 7, tableHeaderStyle, "本期业绩完成情况");

        XSSFFont fontBody = wb.createFont();
        fontBody.setFontHeightInPoints((short) 11);
        XSSFCellStyle styleBody = wb.createCellStyle();
        styleBody.setFont(fontBody);
        // 下边框
        styleBody.setBorderBottom(BorderStyle.THIN);
        // 左边框
        styleBody.setBorderLeft(BorderStyle.THIN);
        // 右边框
        styleBody.setBorderRight(BorderStyle.THIN);
        // 上边框
        styleBody.setBorderTop(BorderStyle.THIN);
        styleBody.setAlignment(HorizontalAlignment.CENTER);
        styleBody.setVerticalAlignment(VerticalAlignment.CENTER);

        for (int i = 0; i < finalResultList.size(); i++) {
            ReportStatisticsVO reportStatisticsVO = finalResultList.get(i);

            XSSFRow trow = sheet.createRow(i + 1);
            trow.setHeightInPoints(24);
            createCell(trow, 0, styleBody, reportStatisticsVO.getAccountName());
            createCell(trow, 1, styleBody, reportStatisticsVO.getCustomerTypeName());
            createCell(trow, 2, styleBody, String.valueOf(reportStatisticsVO.getLevelName()));
            createCell(trow, 3, styleBody, String.valueOf(reportStatisticsVO.getCurCustomerNum() == null ? "" : reportStatisticsVO.getCurCustomerNum()));
            createCell(trow, 4, styleBody, String.valueOf(reportStatisticsVO.getFinalCustomerLevelNum() == null ? "0" : reportStatisticsVO.getFinalCustomerLevelNum()));
            createCell(trow, 5, styleBody, String.valueOf(reportStatisticsVO.getNewCustomerLevelNum() == null ? "0" : reportStatisticsVO.getNewCustomerLevelNum()));
            createCell(trow, 6, styleBody, String.valueOf(reportStatisticsVO.getCurCustomerUpgradeNum() == null ? "0" : reportStatisticsVO.getCurCustomerUpgradeNum()));
            createCell(trow, 7, styleBody, String.valueOf(reportStatisticsVO.getCurCustomerFinishNum() == null ? "0" : reportStatisticsVO.getCurCustomerFinishNum()));
        }

        StringBuilder fileName = new StringBuilder();
        fileName.append("客户信息统计");

        fileName.append("_").append(System.currentTimeMillis());

        //创建临时文件
        File excelFile = File.createTempFile(fileName.toString(), ".xlsx");

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        wb.write(baos);
        FileCopyUtils.copy(baos.toByteArray(), new FileOutputStream(excelFile));

        //保存到临时OSS上
        JsonResult<String> jsonResult = fileGenericStrategy.simpleFileUploadTempBucket("downloadExport", excelFile, fileName + ".xlsx");

        // 删除临时文件
        excelFile.delete();

        if (!jsonResult.getSuccess()) {
            throw new CustomException(jsonResult.getData().toString());
        }


        result.setData(jsonResult.getData());
        result.setMessage("OK");
        result.setStatus(HttpStatus.OK.value());


        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 创建单元格
     *
     * @param row
     * @param column
     * @param style
     * @param value
     */
    private void createCell(XSSFRow row, int column, XSSFCellStyle style, String value) {
        XSSFCell cell = row.createCell(column);
        cell.setCellValue(new XSSFRichTextString(value));
        cell.setCellStyle(style);
    }

}

